package com.example.ksproomdemo

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SnackbarUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.db.RoomUtils
import com.example.ksproomdemo.bean.User
import com.example.ksproomdemo.databinding.ActivityMainBinding
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity : AppCompatActivity() {
    private lateinit var roomUtils: RoomUtils
    private lateinit var binding:ActivityMainBinding
    private val TAG = "Mainivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initView()
        initListener()
    }

    private fun initView() {
        roomUtils = RoomUtils.getInstance()
    }

    private fun initListener() {
        binding.tvAdd.setOnClickListener {
            lifecycleScope.launch {
                val user = User("18","张三","男","zhangsan@qq.com")
                withContext(Dispatchers.IO) {
                    roomUtils.insertUser(user)
                    LogUtils.d(TAG, "----添加用户数据----$user")
                }
                Snackbar.make(binding.tvAdd,user.toString(),Snackbar.LENGTH_LONG).show()
            }
        }
        binding.tvUpdate.setOnClickListener {
            lifecycleScope.launch {
                val user = User("12","Tom","女","ousi@example.com")
                withContext(Dispatchers.IO){
                    roomUtils.updateUser(user)
                    LogUtils.d(TAG, "----更新用户数据----$user")
                }
                Snackbar.make(binding.tvUpdate,user.toString(),Snackbar.LENGTH_LONG).show()
            }
        }
        binding.tvQuery.setOnClickListener {
            lifecycleScope.launch {
                val user = withContext(Dispatchers.IO){
                    RoomUtils.getInstance().getAllUsers()
                }
                LogUtils.d(TAG, "----查询用户数据----$user")
                Snackbar.make(binding.tvQuery,user.toString(),Snackbar.LENGTH_LONG).show()
            }
        }
        binding.tvDelete.setOnClickListener {
            lifecycleScope.launch {
                val user = User("18","张三","男","zhangsan@qq.com")
                withContext(Dispatchers.IO){
                    RoomUtils.getInstance().deleteUser(user)
                }
                LogUtils.d(TAG, "----删除一条用户数据----$user")
                Snackbar.make(binding.tvDelete,user.toString(),Snackbar.LENGTH_LONG).show()
            }
        }
        binding.tvDeleteUsers.setOnClickListener {
            lifecycleScope.launch {
                val user = roomUtils.getAllUsers()
                val user1 : List<User>
                // 删除所有用户
                withContext(Dispatchers.IO) {
                    roomUtils.deleteUsers(user)
                    user1 = roomUtils.getAllUsers()
                }
                LogUtils.d(TAG, "===删除多个用户数据===$user1")
                Snackbar.make(binding.tvDelete,user1.toString(),Snackbar.LENGTH_LONG).show()
            }
        }
        binding.tvDeleteAll.setOnClickListener {
            lifecycleScope.launch {
                val user : List<User>
                // 删除所有用户
                withContext(Dispatchers.IO) {
                    roomUtils.deleteAllUser()
                    user = roomUtils.getAllUsers()
                }
                LogUtils.d(TAG, "===删除所有用户数据===$user")
                Snackbar.make(binding.tvDelete,user.toString(),Snackbar.LENGTH_LONG).show()
            }
        }
    }
}